--[[

	Copyright (c) 2012-2020 baby-bus.com

	TODO:   TouchLayerName
	Author: DemonYao
	Date:   2020.09.29
 
	http://www.babybus.com/ 

]] 




--------------------------
-- 引入命名空间
--------------------------
-- local BaseLayer = import("app.common.layer.BaseLayer")




--[[
	类
--]]


local M = classLayerTouch("Main")




--------------------------
-- 构造函数
--------------------------

function M:ctor(params)
	-- [超类调用]
	M.super.ctor(self, params)
	-- [本类调用]
	self.target            = cc.p(0, 0)
	self.helpPoint         = cc.p(0,0)
	self.dragOffset        = cc.p(0, 0)
	self.dragDisplayObject = nil
end 

--------------------------
-- 渲染
--------------------------

function M:onRender()
	-- [超类调用]
	M.super.onRender(self)
	local bg = U.loadBg("background.png"):to(self)

	-- [本类调用]
	-- 加载节点
	self:loadNodes()
end

-- 加载节点
function M:loadNodes()
	self:loadBoard()
	-- self:loadRobot()
end

-- 加载渲染以外的其他操作
function M:onEnterTransitionFinish()
	--
end

-- 加载机器人
function M:loadRobot()
	dragonBones.CCFactory:loadDragonBonesData("mecha_1406/mecha_1406_ske.json", "test1")
	dragonBones.CCFactory:loadTextureAtlasData("mecha_1406/mecha_1406_tex.json", "test1")
	local armatureDisplay = dragonBones.CCFactory:buildArmatureDisplay("mecha_1406")

	self.chestBone = armatureDisplay:getArmature():getBone("chest");
	self.leftFootBone = armatureDisplay:getArmature():getBone("foot_l");
	self.rightFootBone = armatureDisplay:getArmature():getBone("foot_r");

	armatureDisplay:getAnimation():play("idle");
	self.aimState = armatureDisplay:getAnimation():fadeIn("aim", 0.1, 1, 0, "aim_group");
	self.aimState:setResetToPose(false)
	self.aimState:stop()
	-- armatureDisplay:addTo(self)
	-- armatureDisplay:setPosition(400, 300)
	return armatureDisplay
end

-- 加载滑板
function M:loadBoard()
	self.armatureDisplay = self:loadRobot()

	local bullet = self:createNode("Board", {
		dragonBonesDataFile	= "floor_board/floor_board_ske.json",
		dragonBonesTexFile = "floor_board/floor_board_tex.json",
		armatureName = "floor_board",
		animName = "idle",
	}):p(480, 250):to(self)
	-- 换装（直接拿骨骼去换）牛皮
	bullet:getArmature():getSlot("player"):setDisplay(self.armatureDisplay)	
	self.floorBoard = bullet

	self.circleBone = self.floorBoard:getBone("circle");
	self.floorBoardBone = self.floorBoard:getBone("floor_board");
end




--------------------------
-- 触控
-------------------------- 

function M:onTouchBegan(x, y, touches)
	-- print(math.pi)
	--  触控有效
	self.startP = cc.p(x, y)
	if not self.circle then
		self.circle = self.floorBoard:getSlot("circle"):getDisplay()
	end
	if TH.isTouchInside(self.circle, x, y) then
		self:getCircle(x, y)
	end

	return true 
end

function M:onTouchMoved(x, y, touches)
	self.target.x = x - V.w_2
	self.target.y = V.h_2 - y
	self.disX = x - self.startP.x
	self.disY = y - self.startP.y
	self.startP= cc.p(x, y)

	self:updateAim()
	self:moveCircle(x, y)
end

function M:onTouchEnded(x, y, touches)
	--  
end

function M:upDate()
	scheduler.scheduleUpdateGlobal(function ()

	end)
end

function M:updateAim()
	local positionX = 0--self.floorBoard:px()
	local positionY = 50--self.floorBoard:py()
	local aimOffsetY = self.chestBone.global.y * self.floorBoard:scaleY()
	self.faceDir = self.target.x > 0.0 and 1 or -1;
	self.armatureDisplay:getArmature():setFlipX(self.faceDir < 0)

	if (self.faceDir > 0) then
		self.aimRadian = math.atan2(self.target.y, self.target.x)
	else
		self.aimRadian = math.pi - math.atan2(self.target.y, self.target.x)

		if (self.aimRadian > math.pi) then
			self.aimRadian = self.aimRadian - math.pi * 2.0
		end
	end

	-- // Calculate progress.
	local progress = math.abs((self.aimRadian + math.pi / 2.0) / math.pi)
	-- // Set currentTime.
	self.aimState:setCurrentTime(progress * self.aimState:getTotalTime())
end

function M:updateFoot()

	-- const float Transform::PI = 3.14159265358979323846f;
	-- const float Transform::PI_D = PI * 2.0f;
	-- const float Transform::PI_H = PI * 0.5f;
	-- const float Transform::PI_Q = PI * 0.25f;
	-- const float Transform::DEG_RAD = PI / 180.f;
	-- const float Transform::RAD_DEG = 180.f / PI;

	-- // Set floor board bone offset.
	local minRadian = -25.0 * math.pi / 180;
	local maxRadian = 25.0 *  math.pi / 180;
	local circleRadian = math.atan2(self.circleBone.global.y, self.circleBone.global.x);

	if (self.circleBone.global.x < 0.0) then
		circleRadian = dragonBones.Transform.normalizeRadian(circleRadian + math.pi);
	end

	self.offsetRotation = math.min(math.max(circleRadian, minRadian), maxRadian);
	self.floorBoardBone.offset.rotation = self.offsetRotation;
	self.floorBoardBone:invalidUpdate();
	-- // Set foot bone offset.
	local tan = math.tan(self.offsetRotation);
	local sinR = 1.0 / math.sin(math.pi * 0.5 - self.offsetRotation) - 1.0;

	self.leftFootBone.offset.y = tan * self.leftFootBone.global.x + self.leftFootBone.origin.y * sinR;
	self.leftFootBone.offset.rotation = self.offsetRotation * self.faceDir;
	self.leftFootBone:invalidUpdate();

	self.rightFootBone.offset.y = tan * self.rightFootBone.global.x + self.rightFootBone.origin.y * sinR;
	self.rightFootBone.offset.rotation = self.offsetRotation * self.faceDir;
	self.rightFootBone:invalidUpdate();
end
--------------------------
-- 功能函数
--------------------------

function M:getCircle(x, y)
	if self.dragDisplayObject then return end
	local  circleBone = self.floorBoard:getBone("circle")
	self.helpPoint = self.floorBoard.armatureDisplay:convertToNodeSpace(cc.p(x, y))
	
	
	if circleBone:getOffsetMode() ~= 2 then 
		circleBone.offsetMode = 2
		circleBone.offset.x = circleBone.global.x;
		circleBone.offset.y = circleBone.global.y;
	end

	self.dragOffset.x = circleBone.offset.x - self.helpPoint.x;
	self.helpPoint.y = circleBone.offset.y - self.helpPoint.y;
	local circle = self.floorBoard:getSlot("circle"):getDisplay()
	self.dragDisplayObject = circle
end

function M:moveCircle(x, y)
	if self.dragDisplayObject then
		local  bone = self.floorBoard:getBone("circle")
		if bone then
			self.helpPoint = self.floorBoard.armatureDisplay:convertToNodeSpace(cc.p(x, y))
			bone.offset.x = self.helpPoint.x + self.dragOffset.x;
			bone.offset.y = self.helpPoint.y + self.dragOffset.y;
			bone:invalidUpdate()
			self:updateFoot()
		end
	end
end

--------------------------
-- 属性
--------------------------




--------------------------
-- 父类重写
--------------------------




----------------------------------------------------
-- 析构
--------------------------

function M:onDestructor()
	-- [超类调用]
	M.super.onDestructor(self)
	-- [本类调用]

end








return M






